package srpingmvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
public class StudentController {
    @RequestMapping("/addStudent")
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,Student student) throws Exception {

        ModelAndView mav = new ModelAndView("result");
        mav.addObject("message", "Hello Spring MVC");
        //将数据封装到ModelAndView中
        System.out.println(student.getName());
        System.out.println(student.getAge());
        System.out.println(student.getId());
        mav.addObject("student", student);
        return mav;
    }
//    public ModelAndView getParam(User user, HttpServletRequest request,
//                                 HttpServletResponse response) {
//        System.out.println("Spring RequestParam 传参方式");
//        System.out.println(user.getUserName());
//        System.out.println(user.getPassword());
//        return null;
//    }
//    @RequestMapping(value = "/student", method = RequestMethod.GET)
//    public ModelAndView student() {
//        return new ModelAndView("student", "command", new Student());
//    }
//
//    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
//    @ExceptionHandler({SpringException.class})
//    public String addStudent(@ModelAttribute("HelloWeb") Student student, ModelMap model) {
//        if (student.getName().length() < 5) {
//            throw new SpringException("Given name is too short");
//        } else {
//            model.addAttribute("name", student.getName());
//        }
//        model.addAttribute("age", student.getAge());
//        model.addAttribute("id", student.getId());
//        return "result";
//    }
}
